//发送验证码
function mobile_send_code() {
    let url = "https://h5.ele.me/restapi/eus/login/mobile_send_code";
    r = http.postJson(url, {
        mobile: 手机号,
        captcha_value: "",  //图片验证
        captcha_hash: "",  //图片验证
    }, mobile_send_code_Headers());
    token = r.body.json();
    //log(token['validate_token']);
    return token['validate_token'];  //返回 json {"vailidate_token":"值"}
}

function mobile_send_code_Headers() {
    return {
        headers: {
            'Host': 'h5.ele.me',
            'Connection': 'keep-alive',
            'Content-Length': '61',
            'Origin': 'https://h5.ele.me',
            'User-Agent': 'User-Agent: NOKIA5700/ UCWEB7.0.2.37/28/999',
            'DNT': '1',
            'content-type': 'application/json; charset=utf-8',
            'Accept': '*/*',
            'Referer': 'https://h5.ele.me/login/',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
            'x-shard': 'loc=121.4418213441968,31.2085853330791',       
        }
    }
}

//登陆获取Cookie
function login_by_mobile() {
    let url = "https://h5.ele.me/restapi/eus/login/login_by_mobile";
    r = http.postJson(url, {
        mobile: 手机号,
        validate_code: 验证码,  //验证码
        validate_token: 验证令牌,  // mobile_send_code() 返回值
    }, login_by_mobile_Headers());
    //log(r.headers['set-cookie']);
    return getCookie(r.headers);
}

function login_by_mobile_Headers() {
    return {
        headers: {
            'Host': 'h5.ele.me',
            'Connection': 'keep-alive',
            'Content-Length': '133',
            'Origin': ' https://h5.ele.me',
            'User-Agent': 'User-Agent: NOKIA5700/ UCWEB7.0.2.37/28/999',
            'DNT': '1',
            'content-type': 'application/json; charset=utf-8',
            'Accept': '*/*',
            'Referer': 'https://h5.ele.me/login/',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
            'X-Shard': 'loc=121.4418213441968,31.2085853330791',
        }
    }
}

function getCookie(name) {
    let ck = name['set-cookie'];
    let track_id = ck[0].split(";");
    track_id = track_id[0];
    let USERID = ck[1].split(";");
    USERID = USERID[0];
    let SID = ck[2].split(";");
    SID = SID[0];
    ck = new Array();
    ck[0] = track_id;
    ck[1] = USERID;
    ck[2] = SID;
    //ck = []"\'" + track_id + "\',\'" + USERID + "\',\'" + SID + "\'";
    return ck;  //返回一个数组
}

//设置密码
function set_password(uid,cookie,pwd) {
    let url = "https://restapi.ele.me/eus/v1/users/"+ uid + "/password";
    var r = http.request(url, {
        method: "PUT",
        headers: {
            'content-type': 'application/json; charset=utf-8',
            'cookie': cookie,
        },
        body: '{"password":"' + pwd + '"}',
    });
    let a = r.body.string();
    //log(a);
    if (a == "") {
        log("登陆密码设置：成功！");
    } else {
        log("登陆密码设置：失败！");
    }
}

//账号检测-15
function check(phone) {
    var url = "https://h5.ele.me/restapi/marketing/hongbao/h5/grab";
    var r = http.postJson(url, {
        weixin_uid: "80q7jbhou7j8cpncov6d357imtwws5sd",
        phone: phone,
        group_sn: "8bc2d4df4dd1642f84ce80ae371af033",
    }, ck_Headers());
    try {
        var res = r.body.json();
        var hongbao = res['hongbao_list'][0]['amount'];
        if (hongbao == 15) {
            return 1;
        } else {
            return 0;
        }
    } catch (e) {
        return -1;
    }
}

function ck_Headers() {
    return {
        headers: {
            'Host': 'h5.ele.me',
            'Connection': 'keep-alive',
            'Content-Length': '159',
            'Origin': 'https://h5.ele.me',
            'X-Shard': 'loc=121.4418213441968,31.2085853330791',
            'User-Agent': 'Mozilla/5.0 (Linux; U; Android 7.0; zh-CN; MI 5s Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 Quark/2.4.6.935 Mobile Safari/537.36',
            'DNT': '1',
            'Content-Type': 'text/plain;charset=UTF-8',
            'Accept': '*/*',
            'Referer': 'https://h5.ele.me/baida/?group_sn=8bc2d4df4dd1642f84ce80ae371af033',
            //'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        }
    }
}

//bmob统计
function bmob_register() {
    var aa = device.brand;
    var bb = device.getAndroidId();
    var url = "https://api.bmob.cn/1/classes/thehot";
    var r = http.postJson(url, {
        username: aa,
        password: bb,
    }, bmob_headers());
    return r.body.json();
}

function bmob_headers() {
    var X_Bmob_Application_Id = "a7707bcf4076bf2db49dd7f50ab10c02";
    var X_Bmob_REST_API_Key = "6b7101f058fddf38b19f700290b198b5";
    return {
        headers: {
            'X-Bmob-Application-Id': X_Bmob_Application_Id,
            'Content-Type': 'application/json',
            'X-Bmob-REST-API-Key': X_Bmob_REST_API_Key,
        }
    }
}



